﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using System.Data.Entity;
using kysec.RAL.EFToMySql;
using System.Data.Linq.SqlClient;

namespace kysec_MVC.Controllers
{
    public class CompanyController : Controller
    {
        #region 后台操作显示
        /// <summary>
        /// 实现分页显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult InfoList(int? id, int type)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().RetrieveObject(
                string.Format("Company_{0}_{1}", id, type + DateTime.Now.Second));
            if (o == null)
            {
                using (var db = new kysec.RAL.EFToMySql.MySql_kysecContectionString())
                {
                    PagedList<kysec.RAL.EFToMySql.tb_aboutme> orders = db.tb_aboutme.Where(c => c.c_i_typeId == type).OrderBy(c=>c.c_uq_amId).ToPagedList(id ?? 1, 3);
                    if (Request.IsAjaxRequest())
                        return PartialView("UCCompany", orders);
                    db.Dispose();
                    kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().AddObjectWithAbsoluteTime(
                            string.Format("Company_{0}_{1}", id, type + DateTime.Now.Second),
                            orders,
                            1,
                            System.Web.Caching.CacheItemPriority.Normal);
                    ViewData["type"] = type;
                    return PartialView(orders);
                }
            }
            return PartialView((PagedList<kysec.RAL.EFToMySql.tb_news>)o);
        }

        /// <summary>
        /// 查询具体的某一类信息
        /// </summary>
        /// <param name="type">类别号</param>
        public ActionResult Search(string type,string id)
        {
            List<kysec.RAL.EFToMySql.tb_aboutme> list = kysec.Bll.Company.Search.getAllInfoById(int.Parse(type),id);

            if (list.Count == 0)
                ViewData["error"] = "没有此类信息，快去添加一个吧……";

            return View(list);
        }

        /// <summary>
        /// 添加信息页面跳转  get
        /// </summary>
        public ActionResult AddInfo()
        {
            return View();
        }
        
        /// <summary>
        /// 添加信息具体操作
        /// </summary>
        [ValidateInput(false)][HttpPost]
        public void AddInfo(string type, kysec.RAL.EFToMySql.tb_aboutme model, string c_ntx_content)
        {
            string result = "0";

            if (c_ntx_content.Equals(""))
            {
                Response.Write("<script type='text/javascript'>alert('内容不能为空!')</script>");
                Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/Company/AddInfo?type=" + type);
            }
            else
            {
                model.c_i_typeId = int.Parse(type);
                result = kysec.Bll.Company.aboutme.AddAboutmeInfo(model);

                if (result.Equals("1"))
                {
                    Response.Write("<script type='text/javascript'>alert('添加成功!')</script>");
                    Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/Company/InfoList?type=" + type);
                }
            }
        }
        
        /// <summary>
        /// 修改信息显示
        /// </summary>
        public ActionResult EditInfo(string id,string type)
        {            
            List<kysec.RAL.EFToMySql.tb_aboutme> list = kysec.Bll.Company.Search.getAllInfoById(int.Parse(type),id);

            if (list.Count == 0)
                ViewData["error"] = "数据发生异常，请联系管理员……";
            return View(list);
        }

        /// <summary>
        /// 修改信息具体操作
        /// </summary>
        [ValidateInput(false)][HttpPost]
        public void EditInfo(string id, string type, kysec.RAL.EFToMySql.tb_aboutme model)
        {
            string result = "0";

            result = kysec.Bll.Company.aboutme.UpdateAboutmeInfo(id, model);


            if (result.Equals("1"))
            {
                Response.Write("<script type='text/javascript'>alert('添加成功!')</script>");
                Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/Company/InfoList?type=" + type);
            }
            else {
                Response.Write("<script type='text/javascript'>alert('操作失败!')</script>");            
            }           
        }

        /// <summary>
        /// 设置类型的转换
        /// </summary>
        public void SetType(string id, string type)
        {
            string result = "0";

            result = kysec.Bll.Company.aboutme.SetTypeInfo(id);

            if (result.Equals("1"))
                Response.Write("<script type='text/javascript'>alert('操作成功!')</script>");
            else {
                Response.Write("<script type='text/javascript'>alert('操作失败!')</script>");
            }
             Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/Company/InfoList?type=" + type);
        }

        /// <summary>
        /// 删除和恢复新闻
        /// </summary>
        public void Delete(string id,string type)
        {
            string result = "0";

            result = kysec.Bll.Company.aboutme.DeleteAboutmeInfo(id);

            if (result.Equals("1"))
                Response.Write("<script type='text/javascript'>alert('操作成功!')</script>");
            else
            {
                Response.Write("<script type='text/javascript'>alert('操作失败!')</script>");
            }
            Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/Company/InfoList?type=" + type);

        }
        /// <summary>
        /// 用于从Search查看页面删除
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public string DeleteOne(string id, string type)
        {
            string result = "0";

            result = kysec.Bll.Company.aboutme.DeleteAboutmeInfo(id);

            return result;

        }
        #endregion


        #region 前台页面的显示
        public ActionResult Cmp_Introduction()//公司介绍
        {
            List<kysec.RAL.EFToMySql.tb_aboutme> list = kysec.Bll.Company.Search.getAllInfoForpre(0);

            if (list.Count == 0)
                ViewData["error"] = "数据发生异常，请联系管理员……";
            return View(list);
        }
        public ActionResult Cmp_Culture()//企业文化
        {
            List<kysec.RAL.EFToMySql.tb_aboutme> list = kysec.Bll.Company.Search.getAllInfoForpre(1);

            if (list.Count == 0)
                ViewData["error"] = "数据发生异常，请联系管理员……";
            return View(list);
        }
        public ActionResult Cmp_Honor()//企业荣誉
        {
            List<kysec.RAL.EFToMySql.tb_aboutme> list = kysec.Bll.Company.Search.getAllInfoForpre(2);

            if (list.Count == 0)
                ViewData["error"] = "数据发生异常，请联系管理员……";
            return View(list);
        }
        public ActionResult Cmp_Growing()//成长经历
        {
            List<kysec.RAL.EFToMySql.tb_aboutme> list = kysec.Bll.Company.Search.getAllInfoForpre(3);

            if (list.Count == 0)
                ViewData["error"] = "数据发生异常，请联系管理员……";
            return View(list);
        }
        public ActionResult Investment_Agency()//招商代理
        {
            List<kysec.RAL.EFToMySql.tb_aboutme> list = kysec.Bll.Company.Search.getAllInfoForpre(4);

            if (list.Count == 0)
                ViewData["error"] = "数据发生异常，请联系管理员……";
            return View(list);
        }
        public ActionResult Recruitment_Info()//诚聘英才
        {
            List<kysec.RAL.EFToMySql.tb_aboutme> list = kysec.Bll.Company.Search.getAllInfoForpre(5);

            if (list.Count == 0)
                ViewData["error"] = "数据发生异常，请联系管理员……";
            return View(list);
        }
        public ActionResult Service_Process()//服务流程
        {
            List<kysec.RAL.EFToMySql.tb_aboutme> list = kysec.Bll.Company.Search.getAllInfoForpre(6);

            if (list.Count == 0)
                ViewData["error"] = "数据发生异常，请联系管理员……";
            return View(list);
        }
        public ActionResult Contact_Us()//联系我们
        {
            List<kysec.RAL.EFToMySql.tb_aboutme> list = kysec.Bll.Company.Search.getAllInfoForpre(7);

            if (list.Count == 0)
                ViewData["error"] = "数据发生异常，请联系管理员……";
            return View(list);
        }
        public ActionResult Friend_Contnet()//友情链接
        {
            List<kysec.RAL.EFToMySql.tb_aboutme> list = kysec.Bll.Company.Search.getAllInfoForpre(8);

            if (list.Count == 0)
                ViewData["error"] = "数据发生异常，请联系管理员……";
            return View(list);
        }
        #endregion

    }
}
